#delimit;
** REPLACE FILE PATH WITH PATH TO RELEVANT REPLICATION FILES;
local fileloc = "~/KMS_REPLICATION";
set logtype text;
capture log close importing_weather_data;

log using `fileloc'/log_files/importing_weather_data.txt, name(importing_weather_data) replace;
set more off;
clear;

** Location and weather data obtained from the Global Surface Summary of the Day (see https://data.noaa.gov/dataset/global-surface-summary-of-the-day-gsod);
use `fileloc'/data/location_data/weather_locations.dta, clear;

gen relevant_weather = usaf+"-"+wban;

levelsof relevant_weather, clean local(levels);

clear;
tempfile weather;
save `weather', emptyok;

forvalues year = 2002/2007 {;

	di `year';
	
	quietly {;
	
		foreach weather_id of local levels {;
		
			** Data files compressed using gzip;
			shell gunzip `fileloc'/data/weather_data/`weather_id'-`year'.op.gz;
		
			capture infix
				stn	1-6
				wban	8-12
				year	15-18
				month	19-20
				day	21-22
				temp	25-30
				count_temp	32-33
				dewp	36-41
				count_dewp	43-44
				slp	47-52
				count_slp	54-55
				stp	58-63
				count_stp	65-66
				visib	69-73
				count_visib	75-76
				wdsp	79-83
				count_wdsb	85-86
				mxspd	89-93
				gust	96-100
				max	103-108
			str	flag_max	109-109
				min	111-116
			str	flag_min	117-117
				prcp	119-123
			str	flag_prcp	124-124
				sndp	126-130
				fog	133-133
				rain 134-134
				snow 135-135
				hail 136-136
				thunder 137-137
				tornado 138-138
				using `fileloc'/data/weather_data/`weather_id'-`year'.op, clear;
				
				append using `weather';
				save `weather', replace emptyok;
		
			shell gzip `fileloc'/data/weather_data/`weather_id'-`year'.op;

		};
	
	};

};


save `fileloc'/data/weather_data/daily_weather.dta, replace;

log close importing_weather_data;



